<?php

// 参考：http://www.php.net/manual/zh/pdo.connections.php
// https://segmentfault.com/a/1190000004106175

include_once 'db_config.php';
// array(PDO::ATTR_PERSISTENT => true) 开启持久化连接
$dbh = new PDO('mysql:host=localhost;dbname=wx', $db['user'], $db['pwd'], array(PDO::ATTR_PERSISTENT => true));

// 获取初始年月到当前年月日 http://www.itnose.net/detail/6066165.html
if (empty($_POST["date_start"]) & empty($_POST["date_end"])) {
    // 获取指定日期的净增用户总量
    $next_day = date("Y-m-d", strtotime('-14 days'));
    $end_day = date('Y-m-d', strtotime('-1 days'));
} else {
    $next_day = $_POST['date_start'];
    $end_day = $_POST['date_end'];
}
//while (strtotime($next_day) <= strtotime($end_day)) {
$sql = 'SELECT * FROM `user` where ref_date>="' . $next_day . '" and ref_date<="' . $end_day . '"';
//    $sql = 'SELECT sum(new_user) as new_user,sum(cancel_user) as cancel_user,sum(net_user) as net_user FROM `usersummary` where ref_date="' . $next_day . '"';
    foreach ($dbh->query($sql) as $row) {
        // 把日期和用户数单独组成列 | 变量追加到数组中
        $arr['ref_date'][] = $row['ref_date'];
        $arr['new_user'][] = $row['new_user'];
        $arr['cancel_user'][] = -$row['cancel_user'];
        $arr['net_user'][] = $row['net_user'];
        $arr['cumulate_user'][] = $row['cumulate_user'];
    }

////    $next_day_today = date("Y-m-d", strtotime("+1 days", $next_day));
//    $next_day = date("Y-m-d", strtotime("+1 days", strtotime($next_day)));
//}
$data = json_encode($arr);
echo $data;

// 现在运行完成，在此关闭连接
$dbh = null;
?>